from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
          #文本
        #1文本
        text5 = Tex(
                    r"\text{一阶偏导秘籍}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-39,18,0])
        #2框框标
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=18,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-39,18,0])
        #3箭头
        arrow1 = Arrow(start=[-29,21,0],end=[-25,25.5,0], color=RED
                       , stroke_width=20, tip_length=1)
        arrow2 = Arrow(start=[-29,18,0],end=[-25,18,0], color=RED
                       , stroke_width=20, tip_length=1)
        arrow3 = Arrow(start=[-29,16,0],end=[-25,10.5,0], color=RED
                       , stroke_width=20, tip_length=1)
        #4文本
        text6 = Tex(
                    r"\text{偏导函数}$\dfrac{\partial z}{\partial x},\dfrac{\partial z}{\partial y}$",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,25.5,0])
        text7 = Tex(
                    r"$\dfrac{\partial z}{\partial x}\left| \left( x_{0},y_{0}\right) ,\dfrac{\partial z}{\partial y}\right| \left( x_{0},y_{0}\right) $",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,18,0])
        text8 = Tex(
                    r"\text{补充：隐函数求导公式}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,10.5,0])
        #5箭头
        arrow5 = Arrow(start=[-4,25.5,0],end=[3,25.5,0], color=RED
                       , stroke_width=20, tip_length=1)
        arrow6 = Arrow(start=[5,19,0],end=[10,20,0], color=RED
                       , stroke_width=20, tip_length=1)
        arrow7 = Arrow(start=[5,17,0],end=[10,16,0], color=RED
                       , stroke_width=20, tip_length=1)
        
        text10 = Tex(
                    r"\text{把另外的变量看成常数}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([16,20,0])
        text11 = Tex(
                    r"\text{分段函数}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([16,20,0])
        text12 = Tex(
                    r"\text{连续函数}",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([16,16,0])
        
        self.play(Write(text5),Create(rectangle))
        self.play(Create(arrow1),Create(arrow2),Create(arrow3))
        self.play(Create(text6),Create(text7),Create(text8))
        self.play(Create(arrow6),Create(arrow7))
        self.play(Create(text11),Create(text12))
        #框框闪动

        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=22,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([-15,25.5,0])
        self.play(Create(rectangle1))
        self.play(FadeOut(rectangle1))

        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=30,height=6
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.move_to([-10,18,0])
        self.play(Create(rectangle2))
        self.play(FadeOut(rectangle2))

        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=30,height=6
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle3.move_to([-10,10.5,0])
        self.play(Create(rectangle3))
        self.play(FadeOut(rectangle3))

        
        self.play(
            
            text6.animate.set_color(BLUE)       # 改变颜色动画
        )
        self.play(Create(arrow5))
        text13 = Tex(
                    r"\text{其他变量视为常数}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([16,25.5,0])
        self.play(Write(text13))

        text14 = Tex(
                    r"\text{(1000题1)设函数}$f\left( u\right) $\text{可导,}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,0,0])
        text15 = Tex(
                    r"$z=f\left( \cos y-\cos x\right) +xy,$\text{则}$\dfrac{1}{\sin x}\cdot \dfrac{\partial z}{\partial x}+\dfrac{1}{\sin y}\cdot \dfrac{\partial z}{\partial y}=$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([18,0,0])
        self.play(Write(text14),Write(text15))
        

        text16 = Tex(
                    r"$\dfrac{\partial z}{\partial x}=$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-10,0])
        text17 = Tex(
                    r"$f'\cdot \left( \sin x\right) +y$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text16,RIGHT,buff=1)
        text18 = Tex(
                    r"$\dfrac{1}{\sin x}\cdot \dfrac{\partial z}{\partial x}=f'+\dfrac{y}{\sin x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text17,RIGHT,buff=10)
        self.play(Write(text16),Write(text17))
        self.play(Write(text18))



        text19 = Tex(
                    r"$\dfrac{\partial z}{\partial y}=$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-20,0])
        text20 = Tex(
                    r"$f'\cdot \left( -\sin y\right) +x$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text19,RIGHT,buff=1)
        text21 = Tex(
                    r"$\dfrac{1}{\sin y}\dfrac{\partial z}{\partial y}=-f'+\dfrac{x}{\sin y}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text20,RIGHT,buff=10)
        self.play(Write(text19),Write(text20))
        self.play(Write(text21))

        text22 = Tex(
                    r"$\dfrac{1}{\sin x}\dfrac{\partial z}{\partial x}+\dfrac{1}{\sin y}\dfrac{\partial z}{\partial y}=$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text23 = Tex(
                    r"$\dfrac{y}{\sin x}+\dfrac{x}{\sin x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text22,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text16,text22),
            ReplacementTransform(text17,text22),
            ReplacementTransform(text19,text22),
            ReplacementTransform(text20,text22),
            ReplacementTransform(text18,text23),
            ReplacementTransform(text21,text23)
        )
        self.wait(0.5)
        self.play(Uncreate(text22),Uncreate(text23),Uncreate(text14),Uncreate(text15),
                  text6.animate.set_color(GREEN),
                text13.animate.set_color(GREEN),
                text7.animate.set_color(BLUE),
                text11.animate.set_color(BLUE),
                text12.animate.set_color(BLUE),
                )
        text24 = Tex(
                    r"\text{(定义法)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,20,0])
        text25 = Tex(
                    r"\text{(赋值+求导+赋值)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([37,16,0])
        self.play(Write(text24))
        self.play(Write(text25))
        self.wait(0.5)



        #题目出现
        text26 = Tex(r"\text{(1000题2)设函数}$f\left( x,y\right) =\left| x\right| +y\left| y\right| $\text{,则}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        text27 = Tex(
                   r"\text{(A)}$f_{x}\left( 0,0\right) $\text{存在,}$f_{y}\left( 0,0\right) $\text{存在}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-1,0])
        text28 = Tex(
                    r"\text{(B)}$f_{x}\left( 0,0\right) $\text{存在,}$f_{y}\left( 0,0\right) $\text{不存在}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-1,0])
        text29 = Tex(
                   r"\text{(C)}$f_{x}\left( 0,0\right) $\text{不存在,}$f_{y}\left( 0,0\right) $\text{存在}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-8,0])
        text30 = Tex(
                    r"\text{(D)}$f_{x}\left( 0,0\right) $\text{不存在,}$f_{y}\left( 0,0\right) $\text{不存在}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-8,0])

        self.play(Write(text26),Write(text27),Write(text28),Write(text29),Write(text30))
        self.wait(0.1)

        text31 = Tex(
                    r"$f_{x}\left( 0,0\right) =\lim\limits _{x\rightarrow 0}\dfrac{f\left( x,0\right) -f\left( 0,0\right) }{x}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-13,0])
        text32 = Tex(
                    r"$=\lim\limits _{x\rightarrow 0}\dfrac{\left| x\right| }{x}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text31,RIGHT,buff=1)
        text33 = Tex(
                    r"\text{(不存在)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text32,RIGHT,buff=1)
        self.play(Write(text31))
        self.play(Write(text32))
        self.play(Write(text33))


        text34 = Tex(
                    r"$f_{y}\left( 0,0\right) =\lim\limits _{y\rightarrow 0}\dfrac{f\left( 0,y\right) -f\left( 0,0\right) }{y}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-20,0])
        text35 = Tex(
                    r"$=\lim\limits _{y\rightarrow 0}\left| y\right| =0 $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text34,RIGHT,buff=1)
        text36 = Tex(
                    r"\text{(存在)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text35,RIGHT,buff=1)
        self.play(Write(text34))
        self.play(Write(text35))
        self.play(Write(text36))
         #半透明矩阵扩展
        rectangle4=Rectangle(color=GREEN,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-22,-8,0])
        self.play(
            rectangle4.animate.stretch(39.0,dim=0)
        )

        self.play(Uncreate(text33),Uncreate(text34),Uncreate(text35),Uncreate(text36),
                  Uncreate(text29),Uncreate(text30),Uncreate(text31),Uncreate(text32)
                  ,Uncreate(text26),Uncreate(text27),Uncreate(text28),FadeOut(rectangle4)
                )




        text37 = Tex(
                    r"\text{(1000题3)函数}$z=x^{y}$\text{在点}$\left( 1,2\right) $\text{处的的全微分}$dz=$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        self.play(Write(text37))

        text38 = Tex(
                    r"$ dz| _{\left( 1,2\right) }= z_{x}| \left( 1,2\right) dx+ z_{y}| \left( 1,2\right) dy$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text38))

        #下划线
        #下划线
        dot1=Dot([-8,-2,0])
        dot2=Dot([0,-2,0])
        line=Line(dot1,dot2).set_color(PURPLE)
        line.set_stroke(width=30)  # 设置宽度为 30
        
        dot3=Dot([7,-2,0])
        dot4=Dot([16,-2,0])
        line1=Line(dot3,dot4).set_color(PURPLE)
        line1.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line),Create(line1))

        text39 = Tex(
                    r"$ z_{x}| \left( 1,2\right) =$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-10,0])
        self.play(Write(text39))

        text40 = Tex(
                    r"$ \left( x^{2}\right) ^{'}| x=1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text39,RIGHT,buff=1)
        self.play(Write(text40))
        text41 = Tex(
                    r"$=\left( 2x\right) _{x=1}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text40,RIGHT,buff=1)
        self.play(Write(text41))
        text42 = Tex(
                    r"$=2$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text41,RIGHT,buff=1)
        self.play(Write(text42))


        text43 = Tex(
                    r"$ z_{y}| \left( 1,2\right) =$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-20,0])
        self.play(Write(text43))

        text44 = Tex(
                    r"$ \left( 1^{y}\right) ^{'}| y=2$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text43,RIGHT,buff=1)
        self.play(Write(text44))
        text45 = Tex(
                    r"$=\left( 0\right) _{y=2}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text44,RIGHT,buff=1)
        self.play(Write(text45))
        text46 = Tex(
                    r"$=0$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text45,RIGHT,buff=1)
        self.play(Write(text46))
        text47 = Tex(
                    r"$ dz| \left( 1,2\right) =2dx$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(
            ReplacementTransform(text39,text47),
            ReplacementTransform(text40,text47),
            ReplacementTransform(text41,text47),
            ReplacementTransform(text42,text47),
            ReplacementTransform(text43,text47),
            ReplacementTransform(text44,text47),
            ReplacementTransform(text45,text47),
            ReplacementTransform(text46,text47),
            ReplacementTransform(line1,text47),
            ReplacementTransform(line,text47),
            ReplacementTransform(text38,text47)
        )
        self.wait(0.3)
        self.play(Uncreate(text47),Uncreate(text37),FadeOut(line),FadeOut(line1),Uncreate(text38),
                
                text7.animate.set_color(GREEN),
                text11.animate.set_color(GREEN),
                text12.animate.set_color(GREEN),
                text8.animate.set_color(BLUE),)
        


        text48 = Tex(
                    r"$\dfrac{\partial z}{\partial x}=-\dfrac{Fx}{Fz}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,10.5,0])
        text49 = Tex(
                    r"$\dfrac{\partial z}{\partial y}=-\dfrac{Fy}{Fz}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text48,RIGHT,buff=1)
        
        text50 = Tex(
                    r"$F\left( x,y,z\right) =0,Fz\neq 0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([0,-10,0])
        self.play(Write(text50))
        arrow8 = Arrow(start=[5,10.5,0],end=[12,10.5,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow8))
        self.play(Create(text48),Create(text49))
        self.wait(0.3)
        text51 = Tex(
                    r"\text{(张1000题4)设函数}$z=z\left( x,y\right) $\text{由方程}$\left( x+1\right) z+2y\ln z-\arctan \left( xy\right) =1$\text{确定,则}$ \dfrac{\partial z}{\partial x}| \left( 0,2\right) =$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,2,0])
        self.play(
            ReplacementTransform(text50,text51)
            
        )
        self.wait(0.3)
        text52 = Tex(
                    r"$F=\left( x+1\right) z+2y\ln z-\arctan \left( xy\right) -1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        text53 = Tex(
                    r"$ \dfrac{\partial z}{\partial x}| \left( 0,2\right) =-\dfrac{Fx}{Fz}| \left( 0,2\right) $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-13,0])
        text54 = Tex(
                    r"$=-\dfrac{z-\dfrac{y}{1+\left( xy\right) ^{2}}}{\left( x+1\right) +\dfrac{2y}{z}}| \left( 0,2\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text53,RIGHT,buff=1)
        self.play(Write(text52))
        self.play(Write(text53))
        self.play(Write(text54))

        dot5=Dot([5,3,0])
        dot6=Dot([42,3,0])
        line2=Line(dot5,dot6).set_color(PURPLE)
        line2.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line2))
        text55 = Tex(
                    r"$z=1$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,0,0])
        self.play(Write(text55))
        text56 = Tex(
                    r"$=\dfrac{1}{5}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text54,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text55,text56)
            
        )
        self.wait(0.3)
        self.wait()
        